package com.example.zhuiwei.springbootbase.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class UserController {

    @PostMapping("/user/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        Map<String,Object> map,
                        HttpSession session){
        if (!StringUtils.isEmpty(username)){
            //登入成功后写入到session，session值为空不允许直接访问页面
            session.setAttribute("loginUser",username);
            //防止表单重复提交，可以重定向
            return "redirect:/home.html";
        }else {
            map.put("msg","用户名或密码错误!");
            return "index";
        }
    }
}
